package com.atguigu.order.exception;

import com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.atguigu.common.R;
import com.fasterxml.jackson.databind.json.JsonMapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;

import java.io.PrintWriter;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

@Component
public class MyBlockExceptionHandler  implements BlockExceptionHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest,
                       HttpServletResponse httpServletResponse,
                       String s,
                       BlockException e) throws Exception {


        httpServletResponse.setContentType("application/json;charset=UTF-8");

        PrintWriter writer = httpServletResponse.getWriter();
        JsonMapper mapper = new JsonMapper();

        R error = R.error(s + "被sentinel限制了，原因：" + e.getClass(), null);
        String json = mapper.writeValueAsString(error);
        writer.println(json);
    }
}
